<template>
  <el-card shadow="never" class="aui-card--fill">
    <div class="mod-adx__yunxuanorder}">
      <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList()">
        <el-form-item label="广告位ID">
          <el-input v-model="dataForm.txCpsId" placeholder="广告位ID" clearable></el-input>
        </el-form-item>

        <el-form-item label="订单状态">
        <el-select v-model="dataForm.orderStatus" placeholder="选择运营人员" filterable clearable>
            <el-option
              v-for="status in statusOptions"
              :key="status.value"
              :label="status.label"
              :value="status.value"
            ></el-option>
            </el-select>
        </el-form-item>

        <el-form-item label="支付起始时间">
          <el-date-picker v-model="dataForm.stime" value-format="yyyy-MM-dd" type="date"
            placeholder="起始时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="支付结束时间">
          <el-date-picker v-model="dataForm.etime" value-format="yyyy-MM-dd" type="date"
            placeholder="结束时间">
          </el-date-picker>
        </el-form-item>

        <el-form-item>
          <el-button @click="getDataList()">{{ $t('query') }}</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="info" @click="exportHandle()">{{ $t('export') }}</el-button>
        </el-form-item>
        <el-form-item>
          <el-button v-if="$hasPermission('adx:yunxuanorder:save')" type="primary" @click="addOrUpdateHandle()">{{ $t('add') }}</el-button>
        </el-form-item>
        <el-form-item>
          <el-button v-if="$hasPermission('adx:yunxuanorder:delete')" type="danger" @click="deleteHandle()">{{ $t('deleteBatch') }}</el-button>
        </el-form-item>
      </el-form>
      <el-table v-loading="dataListLoading" :data="dataList" border @selection-change="dataListSelectionChangeHandle" show-summary :summary-method="getSummaries"   style="width: 100%;">
        <el-table-column type="selection" header-align="center" align="center" width="50"></el-table-column>
        <el-table-column prop="id" label="订单编号" header-align="center" align="center"></el-table-column>
        <el-table-column prop="orderId" label="订单号" header-align="center" align="center"></el-table-column>
        <el-table-column prop="orderStatus" label="订单状态" header-align="center" align="center">
          <template slot-scope="scope">
            {{ $getDictLabel("yxuanOrderList", scope.row.orderStatus) }}
          </template>
        </el-table-column>
        <el-table-column prop="remitStatus" label="打款状态" header-align="center" align="center"></el-table-column>
        <el-table-column prop="createTime" label="下单时间" :formatter="formatGmtTime" header-align="center" align="center"></el-table-column>
        <!-- <el-table-column prop="productSource" label="商品标识" header-align="center" align="center"></el-table-column> -->
        <el-table-column prop="orderSource" label="订单来源" header-align="center" align="center">
          <template slot-scope="scope">
            {{ $getDictLabel("yXuanorderSource", scope.row.orderSource) }}
          </template>
        </el-table-column>
        <el-table-column prop="payedTime" label="支付时间"  :formatter="formatGmtTime" header-align="center" align="center"></el-table-column>
        <el-table-column prop="totalCommission" label="总的预估佣金" header-align="center" align="center">
          <template slot-scope="scope">
               <tag>{{scope.row.totalCommission/100}}</tag>
            </template>
        </el-table-column>
        <el-table-column prop="goodsJson"  :show-overflow-tooltip='true' label="商品图片" header-align="center" align="center">
          <template slot-scope="scope">
                 <el-image
                style="width: 100px; height: 100px"
                :src="JSON.parse(scope.row.goodsJson)[0].primary_image_url"
                 :preview-src-list="[JSON.parse(scope.row.goodsJson)[0].primary_image_url]"
                :fit="fit"></el-image>
            </template>
        </el-table-column>
        <el-table-column prop="goodsJson"  :show-overflow-tooltip='true' label="商品价格" header-align="center" align="center">
          <template slot-scope="scope">
                <tag>{{JSON.parse(scope.row.goodsJson)[0].goods_amount/100}}</tag>
            </template>
        </el-table-column>
        <el-table-column prop="goodsJson"  :show-overflow-tooltip='true' label="商品描述" header-align="center" align="center">
          <template slot-scope="scope">
                <tag>{{JSON.parse(scope.row.goodsJson)[0].sku_name_chinese}}</tag>
            </template>
        </el-table-column>
        <!-- <el-table-column prop="cTime" label="创建时间" header-align="center" align="center"></el-table-column> -->
        <el-table-column prop="txCpsId" label="广告位ID" header-align="center" align="center"></el-table-column>
        <!-- <el-table-column :label="$t('handle')" fixed="right" header-align="center" align="center" width="150">
          <template slot-scope="scope">
            <el-button v-if="$hasPermission('adx:yunxuanorder:update')" type="text" size="small" @click="addOrUpdateHandle(scope.row.id)">{{ $t('update') }}</el-button>
            <el-button v-if="$hasPermission('adx:yunxuanorder:delete')" type="text" size="small" @click="deleteHandle(scope.row.id)">{{ $t('delete') }}</el-button>
          </template>
        </el-table-column> -->
      </el-table>
      <el-pagination
        :current-page="page"
        :page-sizes="[10, 20, 50, 100]"
        :page-size="limit"
        :total="total"
        layout="total, sizes, prev, pager, next, jumper"
        @size-change="pageSizeChangeHandle"
        @current-change="pageCurrentChangeHandle">
      </el-pagination>
      <!-- 弹窗, 新增 / 修改 -->
      <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></add-or-update>
    </div>
  </el-card>
</template>

<script>
import mixinViewModule from '@/mixins/view-module'
import AddOrUpdate from './yunxuanorder-add-or-update'
import { getDictDataList } from '@/utils'
import moment from 'moment-timezone';
import { number } from 'echarts';
export default {
  mixins: [mixinViewModule],
  data () {
    return {
      mixinViewModuleOptions: {
        getDataListURL: '/adx/yunxuanorder/page',
        getDataListIsPage: true,
        exportURL: '/adx/yunxuanorder/export',
        deleteURL: '/adx/yunxuanorder',
        deleteIsBatch: true
      },
      dataForm: {
        externalOrderId: ''
      },
      statusOptions: [
        { value: 'CREATED', label: '待付款（已下单）' },
        { value: 'DELIVER_PENDING', label: '待发货（已付款）' },
        { value: 'SENT', label: '待收货（已发货）' },
        { value: 'SUCCESS', label: '交易完成（确认收货）' },
        { value: 'CANCEL', label: '已取消' },
      ],
    }
  },
  components: {
    AddOrUpdate
  },
  methods: {
    formatGmtTime(row, column, cellValue) {
      const shanghaiTime = moment.tz(Number(cellValue), "GMT").tz("Asia/Shanghai");
      return shanghaiTime.format("YYYY-MM-DD HH:mm:ss");
    },
    getSummaries(param) {
      const { columns, data } = param;
      const sums = [];
      columns.forEach((column, index) => {
        if (index === 1) {
          sums[index] = '数据合计';
          return;
        }
        const values = data.map(item => Number(item[column.property]));
        if (column.property === 'profit' || column.property === 'totalCommission') {
          if (!values.every(value => isNaN(value))) {
            sums[index] = values.reduce((prev, curr) => {
              const value = Number(curr);
              if (!isNaN(value)) {
                return Math.floor((prev + curr) * 100) / 100;
              } else {
                return prev;
              }
            }, 0);
            sums[index] += '';
          } else {
            sums[index] = '---'
          }
        }
      
      });
      return sums/100;
    },
  }
}
</script>
